const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    diqu: ['请选择地区'],
    yongrendanwei: ['请选择用人单位'],
    zhaolujiguan: ['请选择招录机关'],
    zhiweidaima: ['请选择职位代码'],
    phone: 0,
    theList: '',
    fenshu: '',
    xingce: '',
    shenlun: '',
    area: '',
    geneal: '',
    paiming: '1',
    fencha: '0',
    page: 29
  },

  toShoulei: function () {
    wx.navigateToMiniProgram({
      appId: 'wxca86930ec3e80717', //模考appid
      path: 'page/course/details?fenxiang=1&id=212314', //模考
      envVersion: 'release', //develop（开发版），trial（体验版），release（正式版） 
      success(res) {
        console.log('打开成功！')
      }
    })
  },


  onLoad: function (options) {
    var _ = this;
    var phone = wx.getStorageSync('phone')
    if (phone) {
      _.setData({
        phone: phone
      })
    }
    _.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      _.setData({
        page: options.page
      })
    }


    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=45145&callback=?', //接收url
      data: {
        level: '1',
        diqu: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择地区')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].diqu)
        }
        _.setData({
          diqu: list
        });
      }
    })
  },
  tijiao: function (res) {

    var _ = this;
    var phone = _.data.phone;
    var diqu = _.data.diqu[_.data.index];
    var yongrendanwei = _.data.yongrendanwei[_.data.index2];
    var zhaolujiguan = _.data.zhaolujiguan[_.data.index3];
    var zhiweidaima = _.data.zhiweidaima[_.data.index4];
    var fenshu = _.data.xingce / 2 + _.data.shenlun / 2;
    if(fenshu > 100){
      wx.showToast({
        title: '分数输入有误，请检查后重新输入',
        icon:'none'
      })
      return false
    }
    if(_.data.xingce > 100){
      wx.showToast({
        title: '分数输入有误，请检查后重新输入',
        icon:'none'
      })
      return false
    }
    if(_.data.shenlun > 100){
      wx.showToast({
        title: '分数输入有误，请检查后重新输入',
        icon:'none'
      })
      return false
    }
    wx.request({ //发送请求
      url: "https://zg99.offcn.com/index/shaifen/register/?actid=45145&callback=?",
      data: {
        phone: phone,
        diqu: diqu,
        yongrendanwei: yongrendanwei,
        zhaolujiguan: zhaolujiguan,
        zhiweidaima: zhiweidaima,
        xingce: _.data.xingce,
        shenlun: _.data.shenlun,
        fenshu: fenshu
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        };
         console.log(theJson)

        wx.request({ //发送请求
          url: "https://zg99.offcn.com/index/shaifen/getphoneinfo/?actid=45145&callback=?",
          data: {
            phone: phone,
            diqu: diqu,
            yongrendanwei: yongrendanwei,
            zhaolujiguan: zhaolujiguan,
            zhiweidaima: zhiweidaima,
            limits: 100
          }, //接收参数
          success(res) { //返回
            var jsonStr = res.data;
            jsonStr = jsonStr.replace("(", "");
            jsonStr = jsonStr.replace(")", "");
            if (typeof jsonStr != 'object') {
              jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
              var theJson = JSON.parse(jsonStr);
            };
            console.log(theJson)

            _.setData({
              lists: theJson.lists,
              theGw: theJson.users
            })
            wx.pageScrollTo({
              scrollTop: 1500,
              duration: 300
            });
          }
        })
        // wx.request({ //发送请求
        //   url: "https://zg99.offcn.com/index/shaifen/getlist/?actid=45145&callback=?",
        //   data: {
        //     diqu: diqu,
        //     yongrendanwei: yongrendanwei,
        //     zhaolujiguan: zhaolujiguan,
        //     zhiweidaima: zhiweidaima,
        //     limits: 100
        //   }, //接收参数
        //   success(res) { //返回
        //     var jsonStr = res.data;
        //     jsonStr = jsonStr.replace("(", "");
        //     jsonStr = jsonStr.replace(")", "");
        //     if (typeof jsonStr != 'object') {
        //       jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
        //       var theJson = JSON.parse(jsonStr);
        //     };
        //     console.log(theJson)
        //     _.setData({
        //       paiming: theJson.lists.length
        //     })
        //   }
        // })
      }
    })
  },




  bindPickerChange: function (e) {
    let num = e.detail.value;
    let _ = this;
    console.log('diqu发送选择改变，携带值为', _.data.diqu[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=45145&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'diqu',
        grtext: _.data.diqu[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择用人单位')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].yongrendanwei)
        }
        _.setData({
          yongrendanwei: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let _ = this;
    console.log('yongrendanwei发送选择改变，携带值为', _.data.yongrendanwei[num2]);
    this.setData({
      index2: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=45145&callback=?', //接收url
      data: {
        level: '3',
        grfiled: 'yongrendanwei',
        grtext: _.data.yongrendanwei[num2],
        onefiled: 'diqu',
        onetext: _.data.diqu[this.data.index]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择招录机关')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].zhaolujiguan)

        }
        _.setData({
          zhaolujiguan: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num3 = e.detail.value;
    let _ = this;
    console.log('zhaolujiguan发送选择改变，携带值为', _.data.zhaolujiguan[num3]);
    this.setData({
      index3: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=45145&callback=?', //接收url
      data: {
        level: '4',
        grfiled: 'zhaolujiguan',
        grtext: _.data.zhaolujiguan[num3],
        onefiled: 'diqu',
        onetext: _.data.diqu[this.data.index],
        twofiled: 'yongrendanwei',
        twotext: _.data.yongrendanwei[this.data.index2],
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        var list = [];
        list.push('请选择职位代码')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].zhiweidaima)
        }
        _.setData({
          zhiweidaima: list
        });
      }
    })
  },
  bindPickerChange4: function (e) {
    let num4 = e.detail.value;
    let _ = this;
    console.log('zhiweidaima发送选择改变，携带值为', _.data.zhiweidaima[num4]);
    this.setData({
      index4: e.detail.value
    })
  },
  xingceInput: function (e) {
    this.setData({
      xingce: e.detail.value
    })
    console.log(this.data.xingce)
  },
  shenlunInput: function (e) {
    this.setData({
      shenlun: e.detail.value
    })
    console.log(this.data.shenlun)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: '2021山东省考查排名知分差',
      path: 'pages/shaifen/index/index?area=' + this.data.area + '&agent=' + this.data.geneal + '&page=' + this.data.page 
    }
  },
  /**分享到朋友圈*/
  onShareTimeline: function () {
    return {
      title: '2021山东省考查排名知分差',
      path: 'pages/shaifen/index/index?area=' + this.data.area + '&agent=' + this.data.geneal + '&page=' + this.data.page
    }
  }
})